package com.example.entity;

/**
 * @Author: wanqian
 * @Date: 2021/1/26 23:49
 */
public class LinkedListStack {

    private LinkedNode head;

    private int size;

    public LinkedListStack(int size) {
        this.head = new LinkedNode();
        this.size = size;
    }

    public LinkedNode getHead() {
        return head;
    }

    public void setHead(LinkedNode head) {
        this.head = head;
    }

    public boolean isEmpty() {
        return head.getNext() == null;
    }

    public int length() {
        LinkedNode temp = this.head.getNext();
        int num = 0;
        while (temp != null) {
            num++;
            temp = temp.getNext();
        }
        return num;
    }

    public boolean isFull() {
        return length() == size;
    }

    public void list() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return;
        }

        LinkedNode temp = this.head.getNext();
        while (temp != null) {
            System.out.println(temp);
            temp = temp.getNext();
        }
    }

    public LinkedNode pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }

        LinkedNode value = this.head.getNext();
        this.head.setNext(head.getNext().getNext());
        return value;
    }

    public void push(LinkedNode node) {
        if (isFull()) {
            System.out.println("栈已满");
            return;
        }

        node.setNext(head.getNext());
        head.setNext(node);
    }
}
